<template>
  <admin-layout>
    <div style="display: flex; justify-content: space-between">
      <h1>文章管理</h1>
      <el-button @click="$router.push({ name: 'ArticleCreate' })">创建</el-button>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%"
      empty-text="暂无数据"
      border
      v-loading="isLoading"
    >
      <el-table-column
        label="#"
        prop="id"
        width="100"
        align="center"
      />
      <el-table-column label="标题" prop="title" />
      <el-table-column label="描述" prop="desc" />
      <el-table-column
        label="发布者"
        prop="author"
        align="center"
        width="100"
      >
        <template #default="scope">
          <el-tag>{{ scope.row.author }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        width="130"
        align="center"
      >
        <template #default="scope">
          <i class="el-icon-time"></i>
          {{ scope.row.created_at }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="140"
      >
        <template #default="scope">
          <el-button
            size="mini"
            circle
            icon="el-icon-view"
            @click="handleShow(scope.row.id)"
          ></el-button
          >
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            circle
            @click="handleEdit(scope.row.id)"
          ></el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            circle
            @click="handleDelete(scope.row.id)"
          ></el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div>
      <el-pagination
        background
        layout="prev, pager, next"
        :page-count="10"
        :pager-count="5"
        :current-page="page"
        prev-text="上一页"
        next-text="下一页"
        @current-change="currentChange"
      >
      </el-pagination>
    </div>
  </admin-layout>
</template>

<script>
import { mapState, mapActions, mapMutations } from 'vuex'

export default {
  name: "Home",
  data() {
    return {
      isLoading: false,
    };
  },
  computed: {
    ...mapState('article', ['tableData', 'page'])
  },
  async created() {
    this.init()
  },
  methods: {
    ...mapActions('article', ['getList', 'delArticle']),
    ...mapMutations('article', ['SET_PAGE']),
    async currentChange(page) {
      // 改变页码
      this.SET_PAGE(page)
      // this.$store.commit('article/SET_PAGE', page)
      // 刷新数据
      this.init()
    },
    async init() {
      try {
        await this.getList()
      } catch (e) {
        this.$message.error('网络错误')
      } finally {
        this.isLoading = false
      }
    },
    handleShow(id) {
      this.$router.push({
        name: 'ArticleShow',
        params: {
          id
        }
      })
    },
    handleEdit(id) {
      this.$router.push({
        name: 'ArticleUpdate',
        params: {
          id
        }
      })
    },
    handleDelete(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await this.delArticle(id)
          await this.init()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        } catch (e) {
          this.$message({
            type: 'error',
            message: '删除失败'
          });
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  },
};
</script>

<style lang="less" scoped>
.el-header {
  background-color: #ff9796;
  display: flex;
  align-items: center;

  ul {
    display: flex;
    align-items: center;
    font-size: 20px;
    margin: 0;
    list-style: none;

    li {
      padding: 0 10px;
    }
  }
}
.el-aside {
  background-color: #42b983;
}

.el-main {
  height: calc(100vh - 60px);
}
</style>
